package com.dyb.recuitment.controller;


import com.dyb.recuitment.common.Result;
import com.dyb.recuitment.entity.Admin;
import com.dyb.recuitment.entity.User;
import com.dyb.recuitment.service.IAdminService;
import com.dyb.recuitment.service.IUserLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 管理员用户表 前端控制器
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-02-21
 */
@RestController
@Api(tags = "管理员管理(Admin)")
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IUserLogService userLogService;

    @ApiOperation(value = "获取用户信息(用户ID)", notes = "通过账号ID获取用户信息 {\"id\":1}")
    @ApiImplicitParam(name = "admin", value = "Admin类")
    @PostMapping("/getUserById")
    public Object getUserById(@RequestBody Admin admin) {
        Object data = null;
        try {
            data = adminService.getByAdmin(admin);
            if (data == null) {
                return Result.warning(data, "无此用户!");
            }
            return Result.success(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "获取用户信息(用户名)", notes = "通过用户名获取用户信息 {\"username\":\"\"}")
    @ApiImplicitParam(name = "admin", value = "Admin类")
    @PostMapping("/getByUsername")
    public Object getByUsername(@RequestBody Admin admin) {
        Object data = null;
        try {
            data = adminService.getByAdmin(admin);
            if (data == null) {
                return Result.warning(data, "无此用户!");
            }
            return Result.success(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "修改用户信息", notes = "修改用户表单信息 {\"\":\"\",\"id\":1}")
    @ApiImplicitParam(name = "admin", value = "Admin类")
    @PostMapping("/updateUser")
    public Object updateUser(@RequestBody Admin admin) {
        Object data = null;
        try {
            data = adminService.updateAdmin(admin);
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "修改失败!");
            }
            return Result.success(data, "修改成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "修改密码", notes = "修改用户密码 {\"password\":\"\",\"id\":1}")
    @ApiImplicitParam(name = "admin", value = "Admin类")
    @PostMapping("/updatePwd")
    public Object updatePwd(@RequestBody Admin admin) {
        Object data = null;
        try {
            data = adminService.updatePwd(admin);
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "修改失败!");
            }
            return Result.success(data, "修改成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "禁用用户", notes = "通过用户ID禁用用户 {\"id\":1}")
    @ApiImplicitParam(name = "admin", value = "Admin类")
    @PostMapping("/deleteUser")
    public Object deleteUser(@RequestBody Admin admin) {
        Object data = null;
        try {
            data = adminService.deleteAdmin(admin);
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "禁用失败!");
            }
            return Result.success(data, "禁用成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "条件查询管理员", notes = "通过条件查询管理员 {\"id\":1}")
    @ApiImplicitParam(name = "admin", value = "Admin类")
    @PostMapping("/selectByUser")
    public Object selectByUser(@RequestBody Admin admin) {
        Object data = null;
        try {
            data = adminService.selectByUser(admin);
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "禁用失败!");
            }
            return Result.success(data, "禁用成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "获取所有用户信息", notes = "获取所有用户信息")
    @GetMapping("/selectAllUser")
    public Object selectAllUser() {
        Object data = null;
        try {
            data = adminService.selectAllAdmin();
            if (data == null || ((List) data).size() <= 0) {
                return Result.warning(data, "用户表为空!");
            }
            return Result.success(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

}
